﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonLibrary.DataModel.Form;
using System.IO;
using System.Xml.Linq;
using ServiceApplication.Service;
using CommonLibrary.Common;
using CommonLibrary.DataModel.Basic;
using ServiceApplication.Service.Basic;

namespace AspWebTest
{
    public partial class FormDr : System.Web.UI.Page
    {
        FormService formService = new FormService();
        SerializeDesigner designer = new SerializeDesigner();
        BasicService basicService = new BasicService();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (!IsOkToSubmit()) return;
            UserEntity userEntity = basicService.QueryUserByLoginid(Session["loginid"].ToString());
            if (FileUpload1.HasFile)
            {
                string filename = FileUpload1.FileName;
                string path = "";
                string BdDefi = "";
                FormEntity formEntity = null;
                path = Server.MapPath("~/uploads");
                FileUpload1.SaveAs(Path.Combine(path, filename));

                XElement xml = XElement.Load(Server.MapPath("~/uploads/" + filename));
                
                //把定义传到前台
                BdDefi = designer.DeserializeXmlToWeb(xml);
                formEntity = designer.DeserializeXmlToFormEntity(xml);
                formEntity.user_name = userEntity.name;
                formEntity.catalogy = "";
                formEntity.createtime = DateTime.Now;
                formEntity.dept_id = -1;
                formEntity.descn = "";
                formEntity.lasttime = DateTime.Now;
                formEntity.location = filename;
                formEntity.user_id = userEntity.id;
                formEntity = formService.AddForm(formEntity);
                Server.Transfer("FormShow.aspx?formid="+formEntity.form_id,true);
            }
        }

        private bool IsOkToSubmit()
        {

            return true;
        }

        protected void BtnRefresh_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.ToString());
        }
    }
}
